/***************************************************************************
*@Author: Nic Wu
*@Created on 18/07/2011
*@Class Name:  SubRegionForecastCtrl
*@Main Methods: Save(), Submit(), Cancel()
*
****************************************************************************/

public with sharing class SubRegionForecastCtrl{

    public String UserSubRegion{get;set;}
    public String UserRegion{get;set;}
    //是否显示Option
    public boolean DisplayOpt{get;set;}
    public boolean DisplayBtn{get;set;}
    public Integer RegionNum{get;set;}
    public List<RollingForecast__c> rollingForecastList{get;set;}
    public List<RollingForecast__c> loadList{get;set;}
    public List<User> UserInfoList{get;set;}
    
     //角色
    public UserRole RoleNMMKTM = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleNMMKTM];
    public UserRole RoleNMKD = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleNMKD ];
    public UserRole RoleNMSD = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleNMSD ];

    //简档
    public Profile ProfileSRMMM = [Select p.Name, p.Id From Profile p Where p.Name =: GlobalProperties.ProfileSRMMM ];

    //constructor
    public SubRegionForecastCtrl(){
    
        UserInfoList = [select Id, Name, UserRegion__c, UserSubRegion__c, UserRoleId, ProfileId,Title__c,email,ManagerId from User where Id =:UserInfo.getUserId()];
        Profile pp  = [select id from profile where name = 'System Administrator'or name =: '系统管理员'];
        //判断是否 NMMM or SRMM
        if(UserInfoList.size()>0){
            //srmm
            if(UserInfoList[0].ProfileId ==ProfileSRMMM.id){
                DisplayBtn = true;
                UserRegion = UserInfoList[0].UserRegion__c;
                UserSubRegion = UserInfoList[0].UserSubRegion__c;
                loadList = [Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, Percentage__c,
                                   Region__c, SubRegion__c, LastActivityDate, Apply_Date__c, ApprovalStatus__c, Approver__c, ForecastCity__c, Month__c, Sales_City__c, UniqueId__c,
                                   UserEmail__c, Year__c, TotalBudgetKG__c, TotalBudgetRMB__c, TotalForecastKG__c, TotalForecastRMB__c, NMKTDirector__c, NMMM__r.email,
                                   NMMM__c, NMSalesDirector__c, RMMM__c, RSM__c, SRMMM__c, Rate__c, ApproveForVF__c FROM RollingForecast__c 
                                   where ApprovalStatus__c =: 'Waiting for SRMMM Approve'];
            }
            //nmmm
            else if(UserInfoList[0].UserRoleId==RoleNMMKTM.id){
                 DisplayOpt = true;
                 DisplayBtn = false;
                 loadList = [Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, Percentage__c, NMKTDirector__r.email,
                                    Region__c, SubRegion__c, LastActivityDate, Apply_Date__c, ApprovalStatus__c, Approver__c, ForecastCity__c, Month__c, Sales_City__c, UniqueId__c,
                                    UserEmail__c, Year__c, TotalBudgetKG__c, TotalBudgetRMB__c, TotalForecastKG__c, TotalForecastRMB__c, NMKTDirector__c, 
                                    NMMM__c, NMSalesDirector__c, RMMM__c, RSM__c, SRMMM__c, Rate__c, ApproveForVF__c FROM RollingForecast__c 
                                    where  ApprovalStatus__c =: 'Waiting for National Medical MKT Manager Approve'];
             
                    appOption.add(new SelectOption(string.valueof(0),'华南区'));
                    appOption.add(new SelectOption(string.valueof(1),'华北区'));
                    appOption.add(new SelectOption(string.valueof(2),'华东区'));
                    appOption.add(new SelectOption(string.valueof(3),'华西区'));
            }
            //nmkt
            else if(UserInfoList[0].UserRoleId==RoleNMKD.id){
                 DisplayOpt = true;
                 DisplayBtn = false;
                 loadList = [Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, Percentage__c,NMKTDirector__r.email,NMSalesDirector__r.email,
                                    Region__c, SubRegion__c, LastActivityDate, Apply_Date__c, ApprovalStatus__c, Approver__c, ForecastCity__c, Month__c, Sales_City__c, UniqueId__c,
                                    UserEmail__c, Year__c, TotalBudgetKG__c, TotalBudgetRMB__c, TotalForecastKG__c, TotalForecastRMB__c, NMKTDirector__c, 
                                    NMMM__c, NMSalesDirector__c, RMMM__c, RSM__c, SRMMM__c, Rate__c, ApproveForVF__c FROM RollingForecast__c 
                                    where  ApprovalStatus__c =: 'Waiting for Medical MKT Director Approve'];
             
                    appOption.add(new SelectOption(string.valueof(0),'华南区'));
                    appOption.add(new SelectOption(string.valueof(1),'华北区'));
                    appOption.add(new SelectOption(string.valueof(2),'华东区'));
                    appOption.add(new SelectOption(string.valueof(3),'华西区'));
            }
            //nmsd
            else if(UserInfoList[0].UserRoleId==RoleNMSD.id){
                 DisplayOpt = true;
                 DisplayBtn = false;
                 loadList = [Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, Percentage__c,NMSalesDirector__r.email,
                                    Region__c, SubRegion__c, LastActivityDate, Apply_Date__c, ApprovalStatus__c, Approver__c, ForecastCity__c, Month__c, Sales_City__c, UniqueId__c,
                                    UserEmail__c, Year__c, TotalBudgetKG__c, TotalBudgetRMB__c, TotalForecastKG__c, TotalForecastRMB__c, NMKTDirector__c, 
                                    NMMM__c, NMSalesDirector__c, RMMM__c, RSM__c, SRMMM__c, Rate__c, ApproveForVF__c FROM RollingForecast__c 
                                    where  ApprovalStatus__c =: 'Waiting for National Medical Sales Director Approve'];
             
                    appOption.add(new SelectOption(string.valueof(0),'华南区'));
                    appOption.add(new SelectOption(string.valueof(1),'华北区'));
                    appOption.add(new SelectOption(string.valueof(2),'华东区'));
                    appOption.add(new SelectOption(string.valueof(3),'华西区'));
            }
            //admin
            else if(UserInfoList[0].ProfileId==pp.id){
                 DisplayOpt = true;
                 DisplayBtn = true;
                 loadList = [Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, Percentage__c,
                                    Region__c, SubRegion__c, LastActivityDate, Apply_Date__c, ApprovalStatus__c, Approver__c, ForecastCity__c, Month__c, Sales_City__c, UniqueId__c,
                                    UserEmail__c, Year__c, TotalBudgetKG__c, TotalBudgetRMB__c, TotalForecastKG__c, TotalForecastRMB__c, NMKTDirector__c, 
                                    NMMM__c, NMSalesDirector__c, RMMM__c, RSM__c, SRMMM__c, Rate__c, ApproveForVF__c FROM RollingForecast__c 
                                    where (ApprovalStatus__c =: 'Waiting for SRMMM Approve' or ApprovalStatus__c =: 'Waiting for National Medical MKT Manager Approve') ];
                     
                appOption.add(new SelectOption(string.valueof(0),'华南区'));
                appOption.add(new SelectOption(string.valueof(1),'华北区'));
                appOption.add(new SelectOption(string.valueof(2),'华东区'));
                appOption.add(new SelectOption(string.valueof(3),'华西区'));
            }
        }
        rollingForecastList = loadList;
    }
    
    List<RollingForecast__c> newList {
        get{
            if(newList == null)
                newList = new List<RollingForecast__c>();
            return newList;   
        }        
        set;
   }
    public PageReference actionSupport() {
        if(loadList.size()>0){
            //华南区
            if(RegionNum==0){
                for(Integer i=0;i<loadList.size();i++){
                    if(loadList[i].Region__c =='a0jO0000000Dsu1'){
                        newList .add(loadList[i]);
                    } 
                }                
            }
            //华北区
            else if(RegionNum==1){
                for(Integer i=0;i<loadList.size();i++){
                    if(loadList[i].Region__c =='a0jO0000000Dsu0'){
                        newList .add(loadList[i]);
                    } 
                }
            }
             //华西区
             else if(RegionNum==2){
                for(Integer i=0;i<loadList.size();i++){
                    if(loadList[i].Region__c =='a0jO0000000Dsu2'){
                        newList .add(loadList[i]);
                    } 
                }
            }
        }
        rollingForecastList = newList;
        return null;
    }
    
    public List<selectOption> appOption{ 
        get{
            if(appOption== null)
                appOption= new List<SelectOption>();
            return appOption;
        }
        set; 
    }

    public PageReference Reject(){
          //srmmm
        if(UserInfoList[0].ProfileId ==ProfileSRMMM.id){
            if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批拒绝'){
                        rollingForecastList[i].ApprovalStatus__c = 'Reject';
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].UserEmail__c );  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' + '您提交的'+ rollingForecastList[i].ForecastCity__c +'滚动预测申请被拒绝!\n' + string.valueof(Date.Today());
                        String Subject = rollingForecastList[i].ForecastCity__c + '滚动预测申请被拒绝';
                        if(toAddresses.size()>0 ){
                                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }
        }
        
        //nmmm
        if(UserInfoList[0].UserRoleId==RoleNMMKTM.id){
             if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批拒绝'){
                        rollingForecastList[i].ApprovalStatus__c = 'Reject';
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].UserEmail__c );  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' + '您提交的'+ rollingForecastList[i].ForecastCity__c +'滚动预测申请被拒绝!\n' + string.valueof(Date.Today());
                        String Subject = rollingForecastList[i].ForecastCity__c + '滚动预测申请被拒绝';
                        if(toAddresses.size()>0 ){
                                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }        
        }
        
         //nmkt
        if(UserInfoList[0].UserRoleId==RoleNMKD.id){
             if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批拒绝'){
                        rollingForecastList[i].ApprovalStatus__c = 'Reject';
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].UserEmail__c);  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' + '您提交的'+ rollingForecastList[i].ForecastCity__c +'滚动预测申请被拒绝!\n' + string.valueof(Date.Today());
                        String Subject = rollingForecastList[i].ForecastCity__c + '滚动预测申请被拒绝';
                        if(toAddresses.size()>0 ){
                                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }        
        }
        
         //nmsd
        if(UserInfoList[0].UserRoleId==RoleNMSD.id){
             if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批拒绝'){
                        rollingForecastList[i].ApprovalStatus__c = 'Reject';
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].UserEmail__c );  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' + '您提交的'+ rollingForecastList[i].ForecastCity__c +'滚动预测申请被拒绝!\n' + string.valueof(Date.Today());
                        String Subject = rollingForecastList[i].ForecastCity__c + '滚动预测申请被拒绝';
                        if(toAddresses.size()>0 ){
                                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }        
        }
        
        pageReference pr = new pageReference('/a1q/o');
        pr.setredirect(true);
        return pr;
    }

    public PageReference Approval(){
        //srmmm
        if(UserInfoList[0].ProfileId ==ProfileSRMMM.id)  {
            if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批通过'){
                        rollingForecastList[i].ApprovalStatus__c = 'Waiting for National Medical MKT Manager Approve';  
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].NMMM__r.email);  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' +  '您收到一个新的'+ rollingForecastList[i].ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
                        String Subject =rollingForecastList[i].ForecastCity__c + '滚动预测审批申请';
                        if(toAddresses.size()>0){
                            EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }
        }

        //nmmm
        if(UserInfoList[0].UserRoleId==RoleNMMKTM.id) {
            if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批通过'){
                        rollingForecastList[i].ApprovalStatus__c = 'Waiting for Medical MKT Director Approve'; 
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].NMKTDirector__r.email);  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' +  '您收到一个新的'+ rollingForecastList[i].ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
                        String Subject = rollingForecastList[i].ForecastCity__c + '滚动预测审批申请';
                        if(toAddresses.size()>0){
                            EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }
        }
        //nmkt
        if(UserInfoList[0].UserRoleId==RoleNMKD.id) {
            if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批通过'){
                        rollingForecastList[i].ApprovalStatus__c = 'Waiting for National Medical Sales Director Approve'; 
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].NMSalesDirector__r.email);  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' +  '您收到一个新的'+ rollingForecastList[i].ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
                        String Subject = rollingForecastList[i].ForecastCity__c + '滚动预测审批申请';
                        if(toAddresses.size()>0){
                            EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }
        }
        
        //nmsd
        if(UserInfoList[0].UserRoleId==RoleNMSD.id) {
            if(rollingForecastList.size()>0){
                for(Integer i=0;i<rollingForecastList.size();i++){
                    if(rollingForecastList[i].ApproveForVF__c == '审批通过'){
                        rollingForecastList[i].ApprovalStatus__c = 'Approve'; 
                        rollingForecastList[i].ApproveForVF__c = '审批通过';
                        List<String> toAddresses = new List<String>();
                        toAddresses.add( rollingForecastList[i].UserEmail__c);  
                        List<String> ccAddresses = new List<String>();
                        String Body = '您好！\n' +  '您收到一个新的'+ rollingForecastList[i].ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
                        String Subject = rollingForecastList[i].ForecastCity__c + '滚动预测审批申请';
                        if(toAddresses.size()>0 ){
                            EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
                        }
                    }
                }
                update rollingForecastList;
            }
        }
        pageReference pr = new pageReference('/a1q/o');
        pr.setredirect(true);
        return pr;
    }

      public PageReference Cancel(){
        pageReference pr = new pageReference('/a1q/o');
        pr.setredirect(true);
        return pr;
    }
}